package cn.jdy.authority.service;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jdy.authority.entity.UserColumn;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.IDUtil;
import cn.jdy.platform.entity.ConfValue;

@Service
public class UserColumnService extends BaseDao<UserColumn> {
	/**
	 * 获得用户文章栏目权限
	 * <p/>
	 * String userid 用户id
	 *
	 * @return List<String> 栏目ID列表
	 * @date 2016-9-13
	 * @author ningqian
	 */
	public List<String> findUserColumn(String userid) {
		List<String> list;
		try {
			Query query = super.em().createSQLQuery("select a.cid from usercolumn a,confvalue b where userid = :userid and a.cid=b.id  ORDER BY cast(b.remark as SIGNED INTEGER)");
			query.setParameter("userid", userid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	/**
	 * 添加用户文章栏目权限
	 */
	@Transactional
	public void addUserColumn(String userid, String cid) {
		String[] cids = cid.split(",");
		try {
			// 删除原有数据
			Query query = super.em().createSQLQuery("delete from usercolumn where userid = :userid");
			query.setParameter("userid", userid);
			query.executeUpdate();

			for(int i = 0; i < cids.length; i++) {
				query = super.em().createSQLQuery("insert into usercolumn (id, userid, cid) values (:id, :userid, :cid)");
				query.setParameter("id", IDUtil.getID());
				query.setParameter("userid", userid);
				query.setParameter("cid", cids[i]);
				query.executeUpdate();
			}
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "addUserColumn:批量添加用户文章栏目权限有误", e);
			throw new BaseException("批量添加用户文章栏目权限  权限有误", e);
		}
	}
	
	
	
	public List<ConfValue> listColumn(int typeid,int pid) {
		List<ConfValue> list;
		try {
			String sql = "select * from confvalue where typeid=:typeid and pid=:pid and id not in(10242,10167,10032,10033,10034,9678,9679,9734,9735)order by sort asc";
			Query query = super.em().createSQLQuery(sql).addEntity(ConfValue.class);
			query.setParameter("typeid", typeid);
			query.setParameter("pid", pid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	public List<ConfValue> xzgajListColumn(int typeid) {
		List<ConfValue> list;
		try {
			String sql = "select * from confvalue where typeid=:typeid and id not in(10409,10410,10411,10412,10413,10414,9795)order by sort asc";
			Query query = super.em().createSQLQuery(sql).addEntity(ConfValue.class);
			query.setParameter("typeid", typeid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	public List<ConfValue> djgajListColumn(int typeid) {
		List<ConfValue> list;
		try {
			String sql = "select * from confvalue where typeid=:typeid and id not in(10409,10410,10411,10414,9795)order by sort asc";
			Query query = super.em().createSQLQuery(sql).addEntity(ConfValue.class);
			query.setParameter("typeid", typeid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	
	public List<ConfValue> lhkgajListColumn(int typeid) {
		List<ConfValue> list;
		try {
			String sql = "select * from confvalue where typeid=:typeid and id not in(10541,10549) order by sort asc";
			Query query = super.em().createSQLQuery(sql).addEntity(ConfValue.class);
			query.setParameter("typeid", typeid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	
	public List<ConfValue> xdgajListColumn(int typeid) {
		List<ConfValue> list;
		try {
			String sql = "select * from confvalue where typeid=:typeid and id not in(10531,10533) order by sort asc";
			Query query = super.em().createSQLQuery(sql).addEntity(ConfValue.class);
			query.setParameter("typeid", typeid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	public List<ConfValue> dzddListColumn(int typeid) {
		List<ConfValue> list;
		try {
			String sql = "select * from confvalue where id not in (select pid from confvalue where typeid=:typeid) and typeid=:typeid1 ORDER BY sort asc";
			Query query = super.em().createSQLQuery(sql).addEntity(ConfValue.class);
			query.setParameter("typeid", typeid);
			query.setParameter("typeid1", typeid);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
	
	public List<String> dzddListColumn_new(String username) {
		List<String> list;
		try {
			String sql = "select a.cid from usercolumn a,confvalue b,user c where a.userid = c.id and c.username=:username and a.cid=b.id  ORDER BY cast(b.remark as SIGNED INTEGER)";
			Query query = super.em().createSQLQuery(sql);
			query.setParameter("username", username);
			list = query.list();
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findUserColumn:获得用户文章栏目权限异常!", e);
			throw new BaseException("获得用户文章栏目权限异常!", e);
		}
		return list;
	}
}
